package edu.mobbuzz.page.menu;

import com.sun.lwuit.Command;
import com.sun.lwuit.Component;
import com.sun.lwuit.Container;
import com.sun.lwuit.Display;
import com.sun.lwuit.Font;
import com.sun.lwuit.Form;
import com.sun.lwuit.Image;
import com.sun.lwuit.Label;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.layouts.BoxLayout;
import edu.mobbuzz.page.menu.inbox.InboxPage;
import edu.mobbuzz.util.string.MenuString;
import edu.mobbuzz.util.theme.Theme;
import java.io.IOException;

public class InboxMenu extends Menu{
    private Form inboxMenuForm;
    private Container inboxMenuPanel; 
    
    private Label menuTitle;
    private Label menuIconLabel;
    private Label menuDescription;
    private Image menuIcon;       
    
    
    
    public InboxMenu(){
        System.gc();
        inboxMenuForm = new Form(){
            public void keyPressed(int keyCode) {
                gameActionPerformed(keyCode);
            }        
        };    
        
        inboxMenuPanel = new Container();
        menuTitle = new Label(getMenuTitle());
        menuDescription = new Label();       
        try {
            menuIcon = Image.createImage("/icons/inbox.gif"); //Theme.getResources().getImage("inbox.png");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        menuIconLabel = new Label(menuIcon);  
       
    }
    
    public void showPage(){ 
        System.gc();
        menuIconLabel.setAlignment(Component.CENTER);
        menuIconLabel.getStyle().setMargin(50, 0, 0, 0);
        menuIconLabel.getStyle().setBgTransparency(0);
        
        menuTitle.setAlignment(Component.LEFT);
        menuTitle.getStyle().setMargin(5, 0, 5, 0);
        menuTitle.getStyle().setFont(Font.createSystemFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_SMALL));
        menuTitle.getStyle().setBgTransparency(0);         
        
        menuDescription.setText("Read your RSS News from your cellphone");      
        menuDescription.setAlignment(Component.LEFT);
        menuDescription.getStyle().setFont(Theme.getFontDescriptionStyle("SansSerif"));
        menuDescription.getStyle().setMargin(5, 0, 5, 0);         
        menuDescription.getStyle().setBgTransparency(0);  
        
        inboxMenuPanel.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
        //inboxMenuPanel.getStyle().setBgImage(Theme.getBackground());      
        inboxMenuPanel.addComponent(menuIconLabel);
        inboxMenuPanel.addComponent(menuTitle);
        inboxMenuPanel.addComponent(menuDescription);
        
        inboxMenuForm.setLayout(new BorderLayout());       
        inboxMenuForm.addComponent(BorderLayout.CENTER, inboxMenuPanel);    
        
        inboxMenuForm.addCommand(getExitCmd());
        inboxMenuForm.setBackCommand(getExitCmd());
        inboxMenuForm.addCommand(getOnlineCmd()); 
        inboxMenuForm.addCommand(new Command("Open") {
            public void actionPerformed(ActionEvent evt) {
               displayInbox();
            }
        });
         
        inboxMenuForm.setCommandListener(getActionlistener());
        inboxMenuForm.setFocusable(true);
        inboxMenuForm.show();
                
    }
    
    public String getMenuTitle() {
        return MenuString.INBOX;
    }   
    
    public void gameActionPerformed(int keyCode){
        System.gc();
        int game = Display.getInstance().getGameAction(keyCode);
        if(game == Display.GAME_RIGHT){
            getInboxTransitionRight();
        }else if(game == Display.GAME_LEFT){
            getInboxTransitionLeft();
        }else if(game == Display.GAME_FIRE){
            displayInbox();
        }
    }
    
    public void getInboxTransitionRight(){
        System.gc();
        inboxMenuForm.setTransitionOutAnimator(Theme.getTransitionOut());
        OutboxMenu outboxMenu = new OutboxMenu();
        outboxMenu.setExitCmd(getExitCmd());
        outboxMenu.setActionlistener(getActionlistener());
        outboxMenu.showPage();
    }
    
    public void getInboxTransitionLeft(){
        System.gc();
        inboxMenuForm.setTransitionOutAnimator(Theme.getTransitionIn());
        ContactMenu contactMenu = new ContactMenu();
        contactMenu.setExitCmd(getExitCmd());
        contactMenu.setActionlistener(getActionlistener());
        contactMenu.showPage();
    }   
    
    public void displayInbox() { 
        System.gc();
        InboxPage inboxPage = new InboxPage();
        inboxPage.setExitCmd(getExitCmd());
        inboxPage.setActionlistener(getActionlistener());
        inboxPage.showPage();
    }

    public void openMessage() {

    }

}
 
